package cn.dengta.webapp.trade.vo;

import java.util.Map;
import java.util.Objects;
import javax.annotation.Nullable;

import cn.dengta.common.model.APIResponse.Field;
import cn.dengta.common.model.*;
import cn.dengta.context.model.Language;
import cn.dengta.context.model.WesKit;
import cn.dengta.context.web.RequestContextImpl;
import cn.dengta.webapp.base.entity.I18nContent;
import cn.dengta.webapp.base.vo.AbstractVO;
import cn.dengta.webapp.trade.entity.Gift;
import cn.dengta.webapp.user.model.BizType;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.StringUtil;
import me.codeplayer.util.X;
import org.apache.commons.lang3.BooleanUtils;

/**
 * 礼物
 */
@Setter
@Getter
@Accessors(chain = true)
public class GiftVO extends AbstractVO<Gift> {

	/** 图标 */
	@Field(name = "图标")
	protected String icon;
	/** 名称 */
	@Field(name = "名称")
	protected Object name;
	@Field(name = "动态文件")
	protected String svga;
	/** 钻石数 */
	@Field(name = "钻石数")
	protected Integer coin;
	/** 链接 */
	@Field(name = "链接")
	protected String url;
	/** 分类 */
	@Field(name = "分类Id")
	protected Long giftCategoryId;
	/** 标签 */
	@Field(name = "标签")
	protected String iconTag;
	/** 是否显示avg 1-是 0-否 */
	@Field(name = "是否显示avg", value = "1-是 0-否")
	protected Integer isAvgView;
	/** 1:全屏 2：半屏 */
	@Field(name = "1:全屏 2：半屏")
	protected Integer actIsScreen;
	@Field(name = "排序")
	protected Integer sort;

	/** 礼物发送次数 */
	@Field(name = "礼物发送次数")
	private Integer sendCount;
	@Field(name = "VIP级别")
	protected Integer vipLevel;
	@Field(name = "显示状态")
	protected State state;
	@Field(name = "分类信息")
	public Map<String, Object> giftCategory;
	@Field(name = "1v1类型")
	public Integer chatOpen;
	@Field(name = "直播类型")
	public Integer liveOpen;
	@Field(name = "快捷礼物")
	public Integer quick;
	/** 获得次数 */
	private Integer gainTimes;
	/** 分包 */
	private String wesKits;
	private transient long giftFlag;

	public static String[] frontList = { "isAvgView", "url", "actIsScreen", "sort", "state", "chatOpen", "liveOpen", "quick" };

	public static String[] msgPush = { "giftCategoryId", "iconTag", "sort", "state" };

	/**
	 * 用于后台页面多语言展示
	 */
	public static GiftVO from(@Nullable Gift source) {
		GiftVO vo = copy(source, GiftVO::new);
		if (vo != null) {
			vo.name = X.map(source.getResolvedLanguageCfg(), I18nContent::getDataMap);
		}
		return vo;
	}

	private static String name(Gift source) {
		Language currentReqLanguage = Language.getCurrentReqLanguage();
		final Language language = currentReqLanguage == null ? Language.en : currentReqLanguage;
		return X.map(source, Gift::getResolvedLanguageCfg, c -> c.get(language));
	}

	public static GiftVO fromFrontList(@Nullable Gift source) {
		GiftVO vo = copy(source, GiftVO::new, frontList);
		if (vo != null) {
			vo.name = StringUtil.toString(name(source));
		}
		return vo;
	}

	public static GiftVO fromMsgPush(@Nullable Gift source) {
		GiftVO vo = copy(source, GiftVO::new, msgPush);
		if (vo != null) {
			vo.name = StringUtil.toString(name(source));
		}
		return vo;
	}

	public Integer getState() {
		return super.stateOutput(state);
	}

	public String getState_(State state) {
		return super.getState_(state);
	}

	public String getVipLevel_() {
		return Status.YES.eq(vipLevel) ? "vip" : null;
	}

	public Integer getChatOpen() {
		if (chatOpen == null) {
			chatOpen = BooleanUtils.toInteger(BizFlag.hasFlag(giftFlag, Gift.BIZ_FLAG_CHAT));
		}
		return chatOpen;
	}

	public Integer getLiveOpen() {
		if (liveOpen == null) {
			liveOpen = BooleanUtils.toInteger(BizFlag.hasFlag(giftFlag, Gift.BIZ_FLAG_LIVE));
		}
		return liveOpen;
	}

	public String getGiftScope_() {
		String giftScope = null;
		if (Status.asResult(getChatOpen())) {
			giftScope = BizType.CHAT.getLabel();
		}
		if (Status.asResult(getLiveOpen())) {
			giftScope = giftScope == null ? BizType.LIVE.getLabel() : giftScope + "、" + BizType.LIVE.getLabel();
		}
		return giftScope;
	}

	public String getQuick_() {
		return X.map(Status.of(quick), Status::getLabel);
	}

	public GiftVO buildGiftTimes(Integer giftTimes) {
		this.gainTimes = giftTimes;
		return this;
	}

	public String getWesKits_() {
		return WesKit.getWesKits_(this.wesKits);
	}

}
